<template>
	<view class="deliveryAddress">
		<view class="line" v-for="(item,index) in pageResult.list" :key="index">
			<view class="address" @click="selFun(item.id,item)">
				<text>{{item.realname}} — {{item.mobile}}</text>
				<text class="text">{{item.address_name}}{{item.address_details}}</text>
			</view>
			<image :src="obtainImgUrl('icon/icon35.png')" mode="aspectFill" @click="jump('addOreditAddress', 'id='+item.id+'&' + optionsParam)"></image>
		</view>
		<view v-if="pageResult.list.length == 0" class="nocontent">
			<image :src="obtainImgUrl('icon/noItem.png')" mode="widthFix"></image>
			<text>暂未找到记录～</text>
		</view>
		<view :class="isOrder ? 'bottom' : 'bottom' " @click="jump('addOreditAddress', 'id=0&' + optionsParam)">新增地址</view>
	</view>
</template>

<script>
	import serve from '../../static/request.js';
	import storage from '../../static/appkey.js';
	import common from '../../static/jump.js';
	export default {
		data() {
			return {
				isOrder: false,
				selId: 0,
				resetKey: 0,
				seldate:[],
				optionsParam: '',
				pageParam: {
					page: 0,
					pageSize: 5
				},
				pageResult: {
					list: [],
					isEmpty: false,
					isEnd: false
				},
			}
		},
		methods: {
			loadList() {
				var self = this;
				self.pageParam.page++
				serve
					.request({
						url: '/member/address/index',
						data: self.pageParam
					})
					.then(res => {
						console.log(res.data); 
						if (res.data && res.data.length > 0) {
							self.pageResult.list=self.pageResult.list.concat(res.data);
						}
						self.pageResult.isEmpty = self.pageResult.list.length == 0;
						self.pageResult.isEnd = res.data.length == 0; 
					});
			},
			delAddress(key) {
				var self = this;
				let delId = this.pageResult.list[key].id;
				uni.showModal({
				    title: '提示',
				    content: '您确定删除这条收货地址吗',
				    success: function (res) {
				        if (res.confirm) {
				            serve.request({
								url: '/member/address/delete',
								data:{
									id:delId,
								}
							}).then(res => {
								uni.showToast({
									title:res.message,
									icon: 'none',
								});
								if(res.code == 200) {
									self.pageResult.list.splice(key,1);
								} 
							});
				        } 
				    }
				});
			},
			confirmAddress() {
				if(this.selId == 0) {
					uni.showToast({
						title: '请选择收货地址',
						icon: 'none',
					});
					return ;
				}
				var pages = getCurrentPages(); // 当前页面
				var beforePage = pages[pages.length - 2]; // 前一个页面
				beforePage.$vm.address= seldate; 
				uni.navigateBack();
			},
			selFun(selId,dat) {
				var pages = getCurrentPages(); // 当前页面
				var beforePage = pages[pages.length - 2]; // 前一个页面
				if(this.isOrder) {
					this.selId = selId;
					this.seldate = dat;
					beforePage.$vm.address= dat; 
					beforePage.$vm.resetAddress(dat);
				} else {
					beforePage.$vm.resetAddress(dat, this.resetKey);
				}
				uni.navigateBack();
			},
			obtainImgUrl(imgUrl) {
				return serve.imgUrl()+imgUrl;
			},
			jump(jump_type, jump_link){
				common.jump(jump_type, jump_link);
			}
		},
		onLoad(options) {
			if(options.type) {
				this.optionsParam = 'type='+options.type+'&data='+options.data;
				this.isOrder = true;
			}
			if(options.resetKey) {
				this.resetKey = options.resetKey;
			}
			console.log('初始');
			this.loadList();
		},
		onPullDownRefresh() {
			console.log('下拉');
			this.pageParam.page=0;
			this.pageResult.list=[];
			this.loadList();
		},
		//触底加载
		onReachBottom() {
			console.log('触底'); 
			if(this.pageResult.isEnd ||this.pageResult.isEmpty){
				return
			}
			this.loadList();
		}
	}
</script>

<style scoped lang="scss">
	.deliveryAddress{
		padding-bottom: 100upx;
		.bottom {
			width: 100%;
			height: 100upx;
			line-height: 100upx;
			text-align: center;
			font-size: 32upx;
			color: rgba(255, 255, 255, 1);
			background: rgba(251, 202, 63, 1);
			bottom: 0upx;
			position: fixed;
		}
		.wid50{
			width: 49.5% !important;
		}
		.line{
			display: flex;
			box-sizing: border-box;
			align-items: flex-start;
			padding: 60upx 30upx;
			position: relative;
			border-bottom: 12upx rgba(252,252,253,1) solid;
			.sel {
				background-image: url(http://app.shanhaiyouyu.com/attachment/xcx/icon/icon37.png);
				background-repeat: no-repeat;
				width: 40upx;
				height: 40upx;
				background-size: 100%;
			}
			
			.nosel {
				width: 36upx;
				height: 36upx;
				border: 1upx solid rgba(60, 72, 104, 1);
				border-radius: 50%;
			}
			.address{
				font-size: 32upx;
				display: flex;
				flex-direction: column;
				width: 550upx;
				margin-left: 23upx;
				font-weight:bold;
				.text{
					margin-top: 32upx;
					font-weight:400;
					color:rgba(60,72,104,0.8);
				}
				
			}
			image{
				position: absolute;
				width:27upx;
				height:33upx;
				top: 130upx;
				right: 30upx;
			}
		}
		.nocontent{
			width: 100%;
			padding-top: 338upx;
			display: flex;
			flex-direction: column;
			align-items: center;
			image{
				width:220upx;
				height:124upx;
			}
			text{
				font-size:28upx;
				color:rgba(204,204,204,1);
			     margin-top: 30upx;
			}
		}
	}
</style>

